/*
 * Pratama Nur Wijaya (c) 2013 
 * 
 * Project       : Sound Of Indonesia
 * Filename      : Scene_Play_Alat_Musik.java
 * Creation Date : Apr 9, 2013 time : 10:04:31 PM
 *
 */

package id.pratama.soundofindonesia.scene;

import java.io.IOException;
import id.pratama.soundofindonesia.IManageableScene;
import id.pratama.soundofindonesia.SceneManager;
import id.pratama.soundofindonesia.Utils;
import id.pratama.soundofindonesia.config.IApps_Data;
import id.pratama.soundofindonesia.config.IApps_Define;
import id.pratama.soundofindonesia.data.IGraphic_Data;
import id.pratama.soundofindonesia.data.ISound_Data;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;

public class Scene_Play_Alat_Musik extends Scene implements IManageableScene, IApps_Data, IApps_Define, IGraphic_Data,
		ISound_Data, IOnAreaTouchListener
{
	private BaseGameActivity			activity;
	private SceneManager				sceneManager;
	private int							type;

	private BitmapTextureAtlas			texMenuBg;
	private ITextureRegion				regMenuBg;
	private Sprite						sprMenuBg;

	private BitmapTextureAtlas			texBtnInfo;
	private ITextureRegion				regBtnInfo;
	private Sprite						sprBtnInfo;

	private BuildableBitmapTextureAtlas	atlasBtnPlayAudio;
	private ITextureRegion				regBtnPlayAudio;
	private Sprite[]					sprBtnPlay;

	public static final int				TYPE_ANGKLUNG	= 0;
	public static final int				TYPE_GAMELAN	= 1;

	private Sound[]						sndAngklung		= new Sound[ANGKLUNG.length];
	private Sound[]						sndGamelan		= new Sound[GAMELAN.length];

	private Font						font;
	private Text						teks;

	private float						marginRight		= 20;

	public Scene_Play_Alat_Musik(BaseGameActivity activity, int type)
	{
		this.activity = activity;
		this.type = type;
		sceneManager = SceneManager.getInstance();

		loadResources();
		applyResources();
		registerUpdateHandler();

	}

	@Override
	public void loadResources()
	{

		if (texMenuBg == null)
			texMenuBg = new BitmapTextureAtlas(activity.getTextureManager(), MENU_UTAMA_BG_TEX_WIDTH,
					MENU_UTAMA_BG_TEX_HEIGHT, Utils.getTextureOptions());
		if (texBtnInfo == null)
			texBtnInfo = new BitmapTextureAtlas(activity.getTextureManager(), MENU_BTN_CREDIT_TEX_WIDTH,
					MENU_BTN_CREDIT_TEX_HEIGHT, Utils.getTextureOptions());
		if (atlasBtnPlayAudio == null)
			atlasBtnPlayAudio = new BuildableBitmapTextureAtlas(activity.getTextureManager(), APPS_BTN_PLAY_TEX_WIDTH,
					APPS_BTN_PLAY_TEX_HEIGHT, Utils.getTextureOptions());

		font = FontFactory.create(activity.getFontManager(), activity.getTextureManager(), 256, 256,
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), Utils.getRatio(50), Color.WHITE);

		regBtnInfo = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(texBtnInfo, activity, MENU_BTN_CREDIT, 0, 0);
		regMenuBg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuBg, activity, MENU_UTAMA_BG, 0, 0);

		regBtnPlayAudio = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasBtnPlayAudio,
				activity.getAssets(), APPS_BTN_AUDIO);

		// Load Sound
		try
		{
			switch (type)
			{
				case TYPE_ANGKLUNG:
					for (int i = 0; i < sndAngklung.length; i++)
					{
						sndAngklung[i] = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity,
								ANGKLUNG[i]);
						Utils.TRACE("Load angklung :" + i);
					}
					break;
				case TYPE_GAMELAN:
					for (int i = 0; i < sndGamelan.length; i++)
					{
						sndGamelan[i] = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity,
								GAMELAN[i]);
						Utils.TRACE("Load gamelan : " + i);
					}

					break;
			}

		} catch (IOException e)
		{
			// TODO: handle exception
			Utils.TRACE(e.toString());
		}

		loadBlackPawn(atlasBtnPlayAudio);
		texMenuBg.load();
		texBtnInfo.load();
		font.load();

	}

	@Override
	public void applyResources()
	{
		if (sprMenuBg == null)
			sprMenuBg = new Sprite(0, 0, RES_WIDTH, RES_HEIGHT, regMenuBg, activity.getVertexBufferObjectManager());
		if (sprBtnInfo == null)
			sprBtnInfo = new Sprite(0, 0, Utils.getRatioWidth(MENU_BTN_CREDIT_WIDTH),
					Utils.getRatioHeight(MENU_BTN_CREDIT_HEIGHT), regBtnInfo, activity.getVertexBufferObjectManager());

		String teksAlatMusik = (type == TYPE_ANGKLUNG) ? "Angklung" : "Gamelan";

		if (teks == null)
			teks = new Text(0, 0, font, teksAlatMusik, activity.getVertexBufferObjectManager());
		teks.setPosition(RES_WIDTH / 2 - teks.getWidth() / 2, Utils.getRatio(10));

		sprBtnInfo.setPosition(RES_WIDTH - sprBtnInfo.getWidth() - Utils.getRatio(10), RES_HEIGHT
				- sprBtnInfo.getHeight() - Utils.getRatio(10));

		attachChild(sprMenuBg);
		this.registerTouchArea(sprBtnInfo);
		sprMenuBg.attachChild(sprBtnInfo);
		sprMenuBg.attachChild(teks);

		setOnAreaTouchListener(this);
		switch (type)
		{
			case TYPE_ANGKLUNG:
				if (sprBtnPlay == null)
					sprBtnPlay = new Sprite[ANGKLUNG.length];
				for (int i = 0; i < ANGKLUNG.length; i++)
				{
					if (sprBtnPlay[i] == null)
						sprBtnPlay[i] = new Sprite(0, 0, Utils.getRatioWidth(APPS_BTN_PLAY_WIDTH),
								Utils.getRatioHeight(APPS_BTN_PLAY_HEIGHT), regBtnPlayAudio,
								activity.getVertexBufferObjectManager());
					sprMenuBg.attachChild(sprBtnPlay[i]);
					if (i == 0)
					{
						sprBtnPlay[i].setPosition(Utils.getRatio(70), RES_HEIGHT / 2
								- sprBtnPlay[i].getHeight() / 2);
					} else
					{
						sprBtnPlay[i]
								.setPosition(
										(sprBtnPlay[i - 1].getX() + sprBtnPlay[i - 1].getWidth())
												+ Utils.getRatio(marginRight), RES_HEIGHT / 2
												- sprBtnPlay[i].getHeight() / 2);
					}
					this.registerTouchArea(sprBtnPlay[i]);
				}

				break;

			case TYPE_GAMELAN:
				if (sprBtnPlay == null)
					sprBtnPlay = new Sprite[GAMELAN.length];
				for (int i = 0; i < GAMELAN.length; i++)
				{
					if (sprBtnPlay[i] == null)
						sprBtnPlay[i] = new Sprite(0, 0, Utils.getRatioWidth(APPS_BTN_PLAY_WIDTH),
								Utils.getRatioHeight(APPS_BTN_PLAY_HEIGHT), regBtnPlayAudio,
								activity.getVertexBufferObjectManager());
					sprMenuBg.attachChild(sprBtnPlay[i]);
					if (i == 0)
					{
						sprBtnPlay[i].setPosition(Utils.getRatio(70), RES_HEIGHT / 2
								- sprBtnPlay[i].getHeight() / 2);
					} else
					{
						sprBtnPlay[i]
								.setPosition(
										(sprBtnPlay[i - 1].getX() + sprBtnPlay[i - 1].getWidth())
												+ Utils.getRatio(marginRight), RES_HEIGHT / 2
												- sprBtnPlay[i].getHeight() / 2);
					}
					this.registerTouchArea(sprBtnPlay[i]);
				}

				break;
		}

	}

	@Override
	public void registerUpdateHandler()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void unloadResources()
	{
		// TODO Auto-generated method stub

	}

	/*
	 * method for loadBlackPawn \m/
	 */
	private void loadBlackPawn(BuildableBitmapTextureAtlas tex)
	{
		try
		{
			tex.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
			tex.load();
		} catch (final TextureAtlasBuilderException e)
		{
			// TODO: handle exception

		}
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		if (SceneManager.currentScene == SceneManager.SCENE_MENU_PLAY_ALAT_MUSIK)
		{
			if (pTouchArea.equals(sprBtnInfo))
			{
				Utils.TRACE("Pencet info alat musik");
				sceneManager.setScene(SceneManager.SCENE_INFO_ALAT_MUSIK, type);
			}

			switch (type)
			{
				case TYPE_ANGKLUNG:
					for (int i = 0; i < ANGKLUNG.length; i++)
					{
						if (pTouchArea.equals(sprBtnPlay[i]))
						{
							Utils.TRACE("Pencet play angklung");
							if (sndAngklung[i] != null)
								sndAngklung[i].play();
						}
					}
					break;
				case TYPE_GAMELAN:
					for (int i = 0; i < GAMELAN.length; i++)
					{
						if (pTouchArea.equals(sprBtnPlay[i]))
						{
							Utils.TRACE("Pencet play gamelan");
							if (sndGamelan[i] != null)
								sndGamelan[i].play();
						}
					}
					break;

			}
		}
		return false;
	}
}
